﻿layui.use(['form', 'layer', 'laydate'], function () {
    var form = layui.form,
       layer = layui.layer,
       laydate = layui.laydate,
       $ = layui.jquery;
    var DeptName, PersonalNumber, PostName, Time, Duties, Require, ApplyName, ApplyTime;
    //执行一个laydate实例
    laydate.render({
        elem: '#test1' //指定元素
        , type: 'datetime'
    });
    //自定义验证
    form.verify({
        Amount: function (value) {
            if (value.length == 0) {
                return '请输入你需要的人数';
            }
        }
        , Duties: function (value) {
            if (value.length == 0) {
                return '请输入你招聘的部门职责';
            }
        }
            , datetimes: function (value) {
                if (value.length == 0) {
                    return '请输入你要求的到岗时间';
                }
            }
        , Require: function (value) {
            if (value.length == 0) {
                return '请输入你招聘的岗位职责';
            }
        }
    });

    //保存
    var loginmanager = {
        //保存按钮
        loginsubmit: function (data) {
            var deptname = $('#deptName option:selected').val();
            var amount = $('#amount').val();
            var PostName = $('#PostName option:selected').val();
            var arrivetime = $('#test1').val();
            var duties = $('#duties').val();
            var require= $('#require').val();
            $.ajax({
                type: "post",
                url: "/Recruitment/RequireAdd",
                data: {
                    "DeptName": deptname,
                    "amount": amount,
                    "PostName": PostName,
                    "arrivetime": arrivetime,
                    "duties": duties,
                    "require": require
                },
                datatype: "json",
                success: function (result) {
                    if (result.state == 1)
                     layer.alert('保存失败'); 
                    else
                      layer.msg(result.Message, { icon: 1 }, function (index) {
                       location.href = '/Recruitment/RequireAdd';
                           
                        });
                }
            });
        }};
    //部门下拉框数据绑定
    function DepartSelect() {
        var url = "/Recruitment/DeptSelect";
        $.post(url, function (result) {
            var deptartments = JSON.parse(result.Data);
            var deptSelect = $("#deptName");
            layui.each(deptartments, function (index, data) {
             
                var deptOption = $("<option value='" + data.Id + "'>" + data.Name + "</option>");
                deptSelect.append(deptOption);
            });
            form.render();
        });
    };
 
    //岗位下拉框数据绑定
    function PostSelect() {
        var url = "/Recruitment/PostSelect";
        $.post(url, function (result) {
            var deptartments = JSON.parse(result.Data);
            var deptSelect = $("#PostName");
            layui.each(deptartments, function (index, data) {
           
                var deptOption = $("<option value='" + data.Id + "'>" + data.PostName + "</option>");
                deptSelect.append(deptOption);
            });
            form.render();
        });
    };
    


    //监听事件
    form.on('submit(update)', function (data) {

        loginmanager.loginsubmit(data);
        return false;
    });
    $(function () {
        DepartSelect();
        PostSelect();
    });

})